<?php
namespace app\admin\controller;

use app\common\models\ArticleCategory;

/**
 * @name 文章管理
 * @desc 文章管理
 * @auth true
 * @author: xujianping(814876569@qq.com)
 * @datetime: 2017/9/18 上午 10:37
 */
class Article extends Common
{
    /**
     * index
     * @author: xujianping(814876569@qq.com)
     * @return mixed
     */
    public function index(){

        $model = new \app\common\models\Article();
        $list = $model->getLists(['status'=>1]);

        return view('',['list'=>$list]);
    }

    /**
     * add
     * @author: xujianping(814876569@qq.com)
     * @return mixed
     */
    public function add(){
        if ($this->request->isPost()){
            $post = $this->request->post();
            $model = new \app\common\models\Article();
            $res = $model->addDataByField($post);
            if ($res){
                $this->success('添加成功','/admin/article');
            }else{
                $this->error('添加失败');
            }
        }else{
            $articleCategory = new ArticleCategory();
            $cateList = $articleCategory->getMany(['status'=>1]);
            return view('',['cateList'=>$cateList]);
        }
    }
    /**
     * edit
     * @author: xujianping(814876569@qq.com)
     * @return \think\response\View
     */
    public function edit(){

        $model = new \app\common\models\Article();

        if ($this->request->isPost()){
            $post = $this->request->post();
            $id = 0;
            if (isset($post['id']) && $post['id'] > 0){
                $id = $post['id'];
            }

            $res = $model->updateDataById($id,$post);
            if ($res !== false){
                $this->success('更新成功','/admin/article');
            }else{
                $this->error('更新失败');
            }
        }else{
            $id = $this->request->get('id',0);
            $info = $model->getDataById($id);
            if (is_null($info)){
                $this->error('数据获取失败');
            }
            $articleCategory = new ArticleCategory();
            $cateList = $articleCategory->getMany(['status'=>1]);
            return view('',['info'=>$info,'cateList'=>$cateList]);
        }
    }

    /**
     * 删除
     */
    public function delete(){
        $id = $this->request->post('id',0);
        $model = new \app\common\models\Article();
        $d = $model->deleteDataById($id);
        if ($d){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}